package nl.ekok.Kentekencheck;

import android.text.InputFilter;
import android.text.Spanned;

/**
 * Filters the input string on valid Dutch car license plate characters
 * @author erickok
 *
 */
public class KentekenInputFilter implements InputFilter {

	public CharSequence filter(CharSequence source, int start, int end,
			Spanned dest, int dstart, int dend) {
		
		// Check for each of the characters...
		String out = "";
		for (int i = start; i < end; i++) {
			
			// If the char is a number or letter
			char c = source.charAt(i);
			if (Character.isDigit(c) || Character.isLetter(c) || c == '-') {
				// Valid, so add it to the output
				out += c;
			}
			
		}
		
		// Add dashes appropriately (AA-00-AA or 0-AAA-00)
		if (out.length() == 6) {
			if ((Character.isDigit(out.charAt(0)) && Character.isDigit(out.charAt(1))) ||
				(Character.isLetter(out.charAt(0)) && Character.isLetter(out.charAt(1)))) {
			out = out.substring(0, 2) + '-' + out.substring(2, 2) + '-' + out.substring(4);
			} else {
				out = out.substring(0, 1) + '-' + out.substring(2, 3) + '-' + out.substring(4);
			}
		}

		// Make all upper case and return the string		
		return out.toUpperCase();
		
	}

}
